package com.ictology.mei;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import com.squareup.picasso.*;


public class NewsActivity_fragment extends Fragment {

    public static NewsActivity_fragment newInstance() {
        NewsActivity_fragment fragment = new NewsActivity_fragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_news_fragment, container, false);
        final ListView listView = (ListView) view.findViewById(R.id.news_listView);
        final ImageView news_cover_imageView = (ImageView) view.findViewById(R.id.news_imageView);

        ArrayList<String> listTitle = new ArrayList<String>();

        final ArrayAdapter<String> titleAdapter = new ArrayAdapter(getActivity() , android.R.layout.simple_list_item_1 ,listTitle);

        listView.setAdapter(titleAdapter);


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document doc = Jsoup.connect("http://www.smes-macao.com/zh-hant/mei/survey.html").get();
                    Element table = doc.select("table.display-table tbody").get(0);
                    Elements tr = table.select("tr");

                    ArrayList<String> title = new ArrayList<String>();
                    final ArrayList<String> link = new ArrayList<String>();

                    final String news_cover_link = String.format("http://www.smes-macao.com%s",tr.select("img").get(1).attr("src"));

                    for(int i=0; i<tr.size(); i++){
                        title.add(tr.get(i).select("td").get(0).select("div").get(0).select("p").get(0).select("a").html().replace("&nbsp;", ""));
                        link.add(String.format("http://www.smes-macao.com%s",tr.get(i).select("td").get(0).select("div").get(0).select("p").get(0).select("a").attr("href")));
                    }

                    final ArrayList<String> titles = title;


                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            System.out.println(news_cover_link);
                            Picasso.with(getActivity()).load(news_cover_link).fit().centerCrop().into(news_cover_imageView);

                            titleAdapter.addAll(titles);
                            titleAdapter.notifyDataSetChanged();

                            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                                    Intent pushViewIntent = new Intent(getActivity(), NewsDetailActivity.class);
                                    pushViewIntent.putExtra("link", link.get(i));
                                    getActivity().startActivity(pushViewIntent);
                                }
                            });
                        }
                    });


                }catch (IOException e){

                }
            }
        }).start();


        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        TextView title=(TextView)getActivity().findViewById(getResources().getIdentifier("action_bar_title", "id", getActivity().getPackageName()));
        title.setText("新聞");
    }

}